﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientRegister
{
    public partial class RemoveCourseOfStuForm : Form
    {

        DataSet ds;

        SynchronizeData sync = new SynchronizeData();

        public RemoveCourseOfStuForm()
        {
            InitializeComponent();
        }


        private void RemoveCourseOfStuForm_Load(object sender, EventArgs e)
        {
            ////Load du lieu tu server
            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Load du lieu khoa hoc
            //sync.GetDataForInsertMethodWithID(sync.CourTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.CourTb, sync.updatecol);

            ////Load du lieu lop hoc
            //sync.GetDataForInsertMethodWithID(sync.ClassTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.ClassTb, sync.updatecol);

            ////Load du lieu lop hoc cua hoc vien
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);
        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {
            CourseDataProvider course = new CourseDataProvider();
            if (txtID.Text != "")
            {
                DataSet ds = course.loadCoursesOfStudent(txtID.Text.ToString());
                DataTable dt = ds.Tables[0];
                cbbxCourse.DataSource = dt;
                cbbxCourse.DisplayMember = "NameCourse";
                cbbxCourse.ValueMember = "ID";
            }
            else
            {
                cbbxCourse.DataSource = null;
            }
        }

        private void cbbxCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbxCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                ds = cls.loadClassOfStudent(txtID.Text.ToString(), cbbxCourse.SelectedValue.ToString());
                GridListClass.DataSource = ds.Tables[0];
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            string xml = "";
            DataTable dtClass = ds.Tables[0];
            string idStu = txtID.Text.ToString();

            StudentInClassDataProvider rmve = new StudentInClassDataProvider();

            for (int i = 0; i < dtClass.Rows.Count; i++)
            {
                string idClass = dtClass.Rows[i].ItemArray[0].ToString();

                xml += rmve.selectClassOfStu(idStu, idClass);
                int result = rmve.RemoveStudentFromClass(idStu, idClass);
                if (result != 1) return;
            }
            //Truyen du lieu len server
            sync.transfDataForDeleteMethod(xml, sync.ClassOfStu);
            MessageBox.Show("Đã xóa !");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }       
    }
}
